﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KMouse
{
    public partial class FormCmdlist : Form
    {
        private Label label_XY;
        private TextBox textBox_Cycle;
        private Button button_BatPath;
        private Button button_Run;
        private Timer timer_Background;
        private System.ComponentModel.IContainer components;
        private TextBox textBox_CmdListShow;
        private Button button_Help;
        private RichTextBox richTextBox_Keyword;
        private RichTextBox richTextBox_Command;
        private TextBox textBox_Interval;
        private Label label_Interval;
        private Label label_Cycle;

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label_XY = new System.Windows.Forms.Label();
            this.textBox_Cycle = new System.Windows.Forms.TextBox();
            this.label_Cycle = new System.Windows.Forms.Label();
            this.button_BatPath = new System.Windows.Forms.Button();
            this.button_Run = new System.Windows.Forms.Button();
            this.timer_Background = new System.Windows.Forms.Timer(this.components);
            this.textBox_CmdListShow = new System.Windows.Forms.TextBox();
            this.button_Help = new System.Windows.Forms.Button();
            this.richTextBox_Keyword = new System.Windows.Forms.RichTextBox();
            this.richTextBox_Command = new System.Windows.Forms.RichTextBox();
            this.textBox_Interval = new System.Windows.Forms.TextBox();
            this.label_Interval = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label_XY
            // 
            this.label_XY.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.label_XY.AutoSize = true;
            this.label_XY.Font = new System.Drawing.Font("Calibri", 9F);
            this.label_XY.Location = new System.Drawing.Point(4, 455);
            this.label_XY.Name = "label_XY";
            this.label_XY.Size = new System.Drawing.Size(66, 14);
            this.label_XY.TabIndex = 133;
            this.label_XY.Text = "(9999,9999)";
            this.label_XY.Click += new System.EventHandler(this.label_XY_Click);
            // 
            // textBox_Cycle
            // 
            this.textBox_Cycle.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox_Cycle.Font = new System.Drawing.Font("Calibri", 9F);
            this.textBox_Cycle.Location = new System.Drawing.Point(171, 450);
            this.textBox_Cycle.Name = "textBox_Cycle";
            this.textBox_Cycle.Size = new System.Drawing.Size(49, 22);
            this.textBox_Cycle.TabIndex = 132;
            this.textBox_Cycle.Text = "5000";
            this.textBox_Cycle.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.textBox_Cycle.TextChanged += new System.EventHandler(this.textBox_Cycle_TextChanged);
            // 
            // label_Cycle
            // 
            this.label_Cycle.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.label_Cycle.AutoSize = true;
            this.label_Cycle.Font = new System.Drawing.Font("Calibri", 9F);
            this.label_Cycle.Location = new System.Drawing.Point(101, 453);
            this.label_Cycle.Name = "label_Cycle";
            this.label_Cycle.Size = new System.Drawing.Size(69, 14);
            this.label_Cycle.TabIndex = 130;
            this.label_Cycle.Text = "Cycle:0000 /";
            // 
            // button_BatPath
            // 
            this.button_BatPath.Font = new System.Drawing.Font("Calibri", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button_BatPath.Location = new System.Drawing.Point(6, 2);
            this.button_BatPath.Name = "button_BatPath";
            this.button_BatPath.Size = new System.Drawing.Size(276, 50);
            this.button_BatPath.TabIndex = 136;
            this.button_BatPath.Text = "Bat Path:(select)";
            this.button_BatPath.UseVisualStyleBackColor = true;
            this.button_BatPath.Click += new System.EventHandler(this.button_BatPath_Click);
            // 
            // button_Run
            // 
            this.button_Run.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button_Run.Font = new System.Drawing.Font("Calibri", 9F);
            this.button_Run.Location = new System.Drawing.Point(222, 450);
            this.button_Run.Name = "button_Run";
            this.button_Run.Size = new System.Drawing.Size(60, 23);
            this.button_Run.TabIndex = 137;
            this.button_Run.Text = "Run";
            this.button_Run.UseVisualStyleBackColor = true;
            this.button_Run.Click += new System.EventHandler(this.button_Run_Click);
            // 
            // timer_Background
            // 
            this.timer_Background.Enabled = true;
            this.timer_Background.Tick += new System.EventHandler(this.timer_Background_Tick);
            // 
            // textBox_CmdListShow
            // 
            this.textBox_CmdListShow.Font = new System.Drawing.Font("Calibri", 9F);
            this.textBox_CmdListShow.Location = new System.Drawing.Point(6, 54);
            this.textBox_CmdListShow.Multiline = true;
            this.textBox_CmdListShow.Name = "textBox_CmdListShow";
            this.textBox_CmdListShow.Size = new System.Drawing.Size(276, 42);
            this.textBox_CmdListShow.TabIndex = 133;
            // 
            // button_Help
            // 
            this.button_Help.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button_Help.Font = new System.Drawing.Font("Calibri", 9F);
            this.button_Help.Location = new System.Drawing.Point(222, 424);
            this.button_Help.Name = "button_Help";
            this.button_Help.Size = new System.Drawing.Size(60, 23);
            this.button_Help.TabIndex = 138;
            this.button_Help.Text = "Help";
            this.button_Help.UseVisualStyleBackColor = true;
            this.button_Help.Click += new System.EventHandler(this.button_Help_Click);
            // 
            // richTextBox_Keyword
            // 
            this.richTextBox_Keyword.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)));
            this.richTextBox_Keyword.DetectUrls = false;
            this.richTextBox_Keyword.Font = new System.Drawing.Font("Calibri", 9F);
            this.richTextBox_Keyword.Location = new System.Drawing.Point(6, 100);
            this.richTextBox_Keyword.Name = "richTextBox_Keyword";
            this.richTextBox_Keyword.Size = new System.Drawing.Size(138, 318);
            this.richTextBox_Keyword.TabIndex = 140;
            this.richTextBox_Keyword.Text = "Keyword1";
            this.richTextBox_Keyword.WordWrap = false;
            // 
            // richTextBox_Command
            // 
            this.richTextBox_Command.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)));
            this.richTextBox_Command.Font = new System.Drawing.Font("Calibri", 9F);
            this.richTextBox_Command.Location = new System.Drawing.Point(150, 100);
            this.richTextBox_Command.Name = "richTextBox_Command";
            this.richTextBox_Command.Size = new System.Drawing.Size(132, 318);
            this.richTextBox_Command.TabIndex = 141;
            this.richTextBox_Command.Text = "Shutdown()";
            this.richTextBox_Command.WordWrap = false;
            // 
            // textBox_Interval
            // 
            this.textBox_Interval.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox_Interval.Font = new System.Drawing.Font("Calibri", 9F);
            this.textBox_Interval.Location = new System.Drawing.Point(197, 425);
            this.textBox_Interval.Name = "textBox_Interval";
            this.textBox_Interval.Size = new System.Drawing.Size(23, 22);
            this.textBox_Interval.TabIndex = 143;
            this.textBox_Interval.Text = "99";
            this.textBox_Interval.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.textBox_Interval.TextChanged += new System.EventHandler(this.textBox_Interval_TextChanged);
            // 
            // label_Interval
            // 
            this.label_Interval.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.label_Interval.AutoSize = true;
            this.label_Interval.Font = new System.Drawing.Font("Calibri", 9F);
            this.label_Interval.Location = new System.Drawing.Point(101, 428);
            this.label_Interval.Name = "label_Interval";
            this.label_Interval.Size = new System.Drawing.Size(99, 14);
            this.label_Interval.TabIndex = 142;
            this.label_Interval.Text = "Interval(x100ms):";
            // 
            // FormCmdlist
            // 
            this.ClientSize = new System.Drawing.Size(287, 477);
            this.ControlBox = false;
            this.Controls.Add(this.textBox_Interval);
            this.Controls.Add(this.label_Interval);
            this.Controls.Add(this.richTextBox_Command);
            this.Controls.Add(this.richTextBox_Keyword);
            this.Controls.Add(this.button_Help);
            this.Controls.Add(this.label_XY);
            this.Controls.Add(this.textBox_CmdListShow);
            this.Controls.Add(this.button_Run);
            this.Controls.Add(this.textBox_Cycle);
            this.Controls.Add(this.button_BatPath);
            this.Controls.Add(this.label_Cycle);
            this.Name = "FormCmdlist";
            this.Text = "Command list";
            this.Load += new System.EventHandler(this.FormCmdlist_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        public FormCmdlist()
        {
            InitializeComponent();
        }

        private void FormCmdlist_Load(object sender, EventArgs e)
        {

        }

        Cmdlist cmd_list = new Cmdlist();
        public Queue<string> queue_cmdlist = new Queue<string>();

        public void Init(Modbus mdbs_s, COM com_s, Form_Main form_main_s)
        {
            button_BatPath.Text = "Bat: " + Param.dict["BAT_PATH"].ToString();
            textBox_Cycle.Text = Param.dict["CMDLIST_CYCLE"].ToString();
            textBox_Interval.Text = Param.dict["CMDLIST_INTERVAL"].ToString();
            richTextBox_Command.Text = Param.dict["CMDLIST_COMMAND"].ToString();
            richTextBox_Keyword.Text = Param.dict["CMDLIST_KEYWORD"].ToString();

            cmd_list.Init(mdbs_s, form_main_s, this, queue_cmdlist);
            cmd_list.BatCall(button_Run, richTextBox_Command, richTextBox_Keyword, textBox_CmdListShow, com_s.serialport);
        }

        private void button_Run_Click(object sender, EventArgs e)
        {
            cmd_list.Run(button_Run, richTextBox_Command, richTextBox_Keyword);
        }

        private void textBox_Cycle_TextChanged(object sender, EventArgs e)
        {
            Param.dict["CMDLIST_CYCLE"] = int.Parse(textBox_Cycle.Text);
        }

        private void textBox_Interval_TextChanged(object sender, EventArgs e)
        {
            Param.dict["CMDLIST_INTERVAL"] = int.Parse(textBox_Interval.Text);
        }

        private void timer_Background_Tick(object sender, EventArgs e)
        {
            if(this.Visible == true)    //窗体显示出来后更刷新
            {
                this.Invoke((EventHandler)(delegate
                {
                    if(richTextBox_Command.Enabled == true)
                    {
                        label_Cycle.Text = "Cycle: " + cmd_list.cycle_cnt.ToString() + " / ";
                    }
                    else
                    {
                        label_Cycle.Text = "Cycle: " + (cmd_list.cycle_cnt + 1).ToString() + " / ";
                    }
                }));

                if(queue_cmdlist.Count > 0)
                {
                    textBox_CmdListShow.AppendText("\r\n" + queue_cmdlist.Dequeue());
                }

                int cur_x, cur_y;
                Cursor cur = Func.GetCursor(out cur_x, out cur_y);
                label_XY.Text = "(" + cur_x.ToString() + "." + cur_y.ToString() + ")";
            }
        }

        private void button_BatPath_Click(object sender, EventArgs e)
        {
            OpenFileDialog bat_path_txt = new OpenFileDialog();
            bat_path_txt.Filter = "bat file|*.bat*";
            bat_path_txt.ValidateNames = true;
            bat_path_txt.CheckPathExists = true;
            bat_path_txt.CheckFileExists = true;
            if(bat_path_txt.ShowDialog() == DialogResult.OK)
            {
                Param.dict["BAT_PATH"] = bat_path_txt.FileName;
                button_BatPath.Text = "Bat: " + bat_path_txt.FileName;
            }
        }

        public void Update_Cmdlist_Parm()
        {
            Param.dict["CMDLIST_COMMAND"] = richTextBox_Command.Text;
            Param.dict["CMDLIST_KEYWORD"] = richTextBox_Keyword.Text;
        }

        private void button_Help_Click(object sender, EventArgs e)
        {
            string HelpStr = "Cmd list: " + "\r\n" + Cmdlist.total_command_str;
            MessageBox.Show(HelpStr, "Help");
        }

        private void label_XY_Click(object sender, EventArgs e)
        {
            Dbg.WriteLine("CPU ID:%", Func.GetCpuID());
        }
    }
}
